import type { INpcData } from '../../data/NpcData';
import { InteractMgr } from '../../mgr/InteractMgr';
import type {
    RemoteData,
    ClientEvents,
    RemoteType,
} from '../../../../../shares/sharesApp';
import { RemoteMgr } from '../../mgr/RemoteMgr';

export class Npc {
    /**
     * NPC的唯一标识
     */
    private id: string = '';
    /**
     * 客户端事件名称
     * @deprecated
     */
    private clientEvent: string = '';
    /**
     * NPC实体
     */
    private entity?: GameEntity;
    /**
     * 新建NPC实例
     */
    constructor() {}

    public start(config: INpcData): void {
        this.id = config.id;
        console.log(`(Server) Npc ${this.id} start`);
        const entity = (this.entity =
            config.entity || world.querySelector(`#${this.id}`) || undefined);
        if (entity) {
            this.entity = entity;
            entity.interactColor.copy(new GameRGBColor(255, 255, 255));
            entity.interactHint = config.interactHint;
            entity.interactRadius = 3;
            // entity.interactSound;
            entity.enableInteract = true;
        }

        this.bindInteractEvents();
    }

    /**
     * 绑定交互事件
     */
    private bindInteractEvents(): void {
        const npc = this.entity || world.querySelector(`#${this.id}`)!;
        if (npc) {
            npc.onInteract((event) => {
                this.onInteract(event);
            });
        }
    }

    /**
     * 处理交互事件
     * @param event 交互事件
     */
    private async onInteract(event: GameInteractEvent): Promise<void> {
        InteractMgr.instance.dialog(this.id, event.entity, event);
    }
}
